<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-title">
        <h2>管理系统登录</h2>
      </div>

      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="form" @keyup.enter="handleLogin">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="User" size="large" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock" size="large"
            show-password />
        </el-form-item>

        <el-form-item>
          <!-- <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox> -->
          <el-checkbox checked="remember" v-model="loginConfig.remember"
            @change="setRememberPassword">记住密码</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="large" class="login-button" :loading="loading" @click="handleLogin">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useStorage } from '@vueuse/core';

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const loginFormRef = ref()
    const loading = ref(false)

    const loginConfig = useStorage('login-config', {
      remember: true,
      username: '', // 演示默认值
      password: '',
    });


    const loginForm = reactive({
      username: loginConfig.value.username,
      password: loginConfig.value.password,
    })

    const setRememberPassword = (value) => {
      loginConfig.value.remember = value;
    };

    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }

    const handleLogin = async () => {
      if (!loginFormRef.value) return

      await loginFormRef.value.validate((valid) => {
        if (valid) {
          loading.value = true

          // 模拟登录请求，暂时跳过API验证
          setTimeout(() => {
            // 保存模拟token到localStorage
            localStorage.setItem('access_token', 'mock-token')
            const { remember } = loginConfig.value;
            loginConfig.value.username = remember ? loginForm.username : '';
            loginConfig.value.password = remember ? loginForm.password : '';
            loading.value = false
            ElMessage.success('登录成功')
            router.push('/')
          }, 1000)
        }
      })
    }

    return {
      loginFormRef,
      loginForm,
      loginRules,
      loginConfig,
      loading,
      handleLogin,
      setRememberPassword
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: url('/images/login-bg.svg') center/cover no-repeat;
  background-color: #f0f2f5;
  /* Fallback color */
}

.login-form {
  width: 400px;
  padding: 40px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
}

.login-title h2 {
  font-size: 24px;
  color: #333;
}

.form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
}
</style>